//
//  DeviceRenameVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/31.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "DeviceRenameVC.h"

@interface DeviceRenameVC () {
    DeviceItem *mDeviceItem;
}

@property(weak, nonatomic) IBOutlet UIButton *backBt;
@property(weak, nonatomic) IBOutlet UIView *accountView;
@property(weak, nonatomic) IBOutlet UITextField *accountTf;
@property(weak, nonatomic) IBOutlet UIButton *operateBt;

@end

@implementation DeviceRenameVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setData:(DeviceItem *) deviceItem {
    mDeviceItem = deviceItem;
}

- (void)initView {
    self.accountView.layer.cornerRadius = globalRadius;
    [[self.backBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [[self.operateBt rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        NSString *name = self.accountTf.text;
        if (name.length<=0) {
            [JPToast showText:@"请输入设备名称"];
            return;
        }
        if (name.length>45) {
            [JPToast showText:@"设备名称不能大于45个字符"];
            return;
        }
        [self request:name];
    }];
}

- (void)request:(NSString *)name {
    [self showLoading:@"提交中"];
    NSString *url = [NSString stringWithFormat:@"%@%@/label", d_label, mDeviceItem.sn];
    NSDictionary *dic = @{@"label": name};
    [[[HttpRequest shareInstance] signPost:url map:[NSMutableDictionary dictionaryWithDictionary:dic]] subscribeNext:^(id x) {
        [self hideLoading];
        if ([x[@"code"] intValue] == 200) {
            [JPToast showText:@"修改成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [JPToast showText:x[@"message"]];
        }
    } error:^(NSError *error) {
        [self hideLoading];
        [JPToast showText:@"网络错误"];
    }];
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end
